//
//  ArtEvent.m
//  SmartRoadShow
//
//  Created by  ibokan on 10-9-25.
//  Copyright 2010 simonwang142232@gmail.com. All rights reserved.
//

#import "ArtEvent.h"
#import "database.h"
//#import "NSDate-Helper.h"
#import "DateUtil.h"

@implementation ArtEvent
@synthesize _id,news_id,_level; 
@synthesize _title,_thumb,_city,_starttime,_endtime,_content,_address,_tel,_googleposi;

+(BOOL) IsExistArtEvent:(NSString *)dateString
{
	
	PLSqliteDatabase *db = [database setup];
	id<PLResultSet> rs;
	
	rs = [db executeQuery:@"select starttime,endtime from ArtEvent"];
	while ([rs next])
	{
		NSString *start = [rs objectForColumn:@"starttime"];
		NSString *end  = [rs objectForColumn:@"endtime"];
				
		if([DateUtil diffBetweenDateStr:dateString andDateStr:start] && [DateUtil diffBetweenDateStr:end andDateStr:dateString])
		{
			return YES;
		}
	}
	return NO;
}

//查询某天有的艺术活动
+(NSArray *) searchAllArtEvents:(NSString *)dateString
{
	PLSqliteDatabase *db = [database setup];
	id<PLResultSet> rs;
	NSMutableArray *artEventArray = [[NSMutableArray alloc] init];
	
	rs = [db executeQuery:@"select * from ArtEvent"];
	while ([rs next])
	{
		int ID = [[rs objectForColumn:@"ID"] intValue];
		int newsId = [[rs objectForColumn:@"news_id"] intValue];
		NSString *title = [rs objectForColumn:@"title"];
		NSString *thumb = [rs objectForColumn:@"thumb"];
		NSString *city = [rs objectForColumn:@"city"];
		int level = [[rs objectForColumn:@"level"] intValue];
		NSString *starttime = [rs objectForColumn:@"starttime"];
		NSString *endtime = [rs objectForColumn:@"endtime"];
		NSString *address = [rs objectForColumn:@"address"];
		NSString *tel = [rs objectForColumn:@"tel"];
		NSString *googleposi = [rs objectForColumn:@"googleposi"];
		NSString *content = [rs objectForColumn:@"content"];
		
		if([DateUtil diffBetweenDateStr:dateString andDateStr:starttime] && [DateUtil diffBetweenDateStr:endtime andDateStr:dateString])
		{
			ArtEvent *artEvent = [[ArtEvent alloc] initWithID:ID
													   NewsID:newsId 
														Title:title
														Thumb:thumb
														 City:city 
														Level:level 
													Starttime:starttime 
													  Endtime:endtime
													  Address:address 
														  Tel:tel
												   Googleposi:googleposi 
													  Content:content];
			[artEventArray addObject:artEvent];
			[artEvent release];
		}
	}
	
	[rs close];
	return [artEventArray autorelease];
}


+(NSArray *) findCity:(NSString *)city Begintime:(NSString *)beginStr Endtime:(NSString *)endStr 
{
	NSLog(@"city:%@  begin:%@   end:%@",city,beginStr,endStr);
	PLSqliteDatabase *db = [database setup];
	id<PLResultSet> rs = nil;
	NSString *sql = nil;
	
	NSMutableArray *findArray = [[NSMutableArray alloc] init];
  
	if([beginStr isEqualToString:@"0000-00-00"])
	{
		if ([city isEqualToString:@"全部"]) {
			sql = [NSString stringWithFormat:@"select * from ArtEvent"];
		}
		else {
			sql = [NSString stringWithFormat:@"select * from ArtEvent where city = '%@'",city];
		}
	}
	else if(![beginStr isEqualToString:@"0000-00-00"])
	{
		if ([city isEqualToString:@"全部"]) {
			sql = [NSString stringWithFormat:@"select * from ArtEvent where (starttime >= '%@' and endtime <= '%@' ) or (starttime <= '%@' and endtime >= '%@') or (starttime <= '%@' and endtime >= '%@') or (starttime <= '%@' and endtime >= '%@')",beginStr,endStr,beginStr,endStr,beginStr,beginStr,endStr,endStr];
		}
		else {
			sql = [NSString stringWithFormat:@"select * from ArtEvent where city = '%@' and ((starttime >= '%@' and endtime <= '%@') or (starttime <= '%@' and endtime >= '%@') or (starttime <= '%@' and endtime >= '%@') or (starttime <= '%@' and endtime >= '%@'))", city,beginStr,endStr,beginStr,endStr,beginStr,beginStr,endStr,endStr];
		}
	}
	rs = [db executeQuery:sql];

	while ([rs next])
	{
		int ID = [[rs objectForColumn:@"ID"] intValue];
		int newsId = [[rs objectForColumn:@"news_id"] intValue];
		NSString *title = [rs objectForColumn:@"title"];
		NSString *thumb = [rs objectForColumn:@"thumb"];
		NSString *city = [rs objectForColumn:@"city"];
		int level = [[rs objectForColumn:@"level"] intValue];
		NSString *starttime = [rs objectForColumn:@"starttime"];
		NSString *endtime = [rs objectForColumn:@"endtime"];
		NSString *address = [rs objectForColumn:@"address"];
		NSString *tel = [rs objectForColumn:@"tel"];
		NSString *googleposi = [rs objectForColumn:@"googleposi"];
		NSString *content = [rs objectForColumn:@"content"];
		
		ArtEvent *artEvent = [[ArtEvent alloc] initWithID:ID NewsID:newsId Title:title Thumb:thumb City:city Level:level Starttime:starttime Endtime:endtime Address:address Tel:tel Googleposi:googleposi Content:content];
		[findArray addObject:artEvent];
		[artEvent release];
	}
	
	[rs close];
	
	return [findArray autorelease];
}




+(NSArray *)findAll
{
	PLSqliteDatabase *db = [database setup];
	id<PLResultSet> rs;
	
	rs = [db executeQuery:@"select * from ArtEvent"];
	NSMutableArray *artEventArray = [[NSMutableArray alloc] init];
	while ([rs next])
	{
		int ID = [[rs objectForColumn:@"ID"] intValue];
		int newsId = [[rs objectForColumn:@"news_id"] intValue];
		NSString *title = [rs objectForColumn:@"title"];
		NSString *thumb = [rs objectForColumn:@"thumb"];
		NSString *city = [rs objectForColumn:@"city"];
		int level = [[rs objectForColumn:@"level"] intValue];
		NSString *starttime = [rs objectForColumn:@"starttime"];
		NSString *endtime = [rs objectForColumn:@"endtime"];
		NSString *address = [rs objectForColumn:@"address"];
		NSString *tel = [rs objectForColumn:@"tel"];
		NSString *googleposi = [rs objectForColumn:@"googleposi"];
		NSString *content = [rs objectForColumn:@"content"];
		
		ArtEvent *artEvent = [[ArtEvent alloc] initWithID:ID NewsID:newsId Title:title Thumb:thumb City:city Level:level Starttime:starttime Endtime:endtime Address:address Tel:tel Googleposi:googleposi Content:content];
		[artEventArray addObject:artEvent];
		[artEvent release];
	}
	
	[rs close];
	
	return [artEventArray autorelease];
}

+(BOOL) findNewsId:(int)newsId
{
	PLSqliteDatabase *db = [database setup];
	id<PLResultSet>rs;
	
	rs = [db executeQuery:@"select news_id from ArtEvent"];
	while ([rs next])
	{
		int newid = [[rs objectForColumn:@"news_id"] intValue];
		
		if(newid == newsId)
		{
			return YES;
		}
	}
	
	return NO;
}


+(BOOL) updateArtEvent:(ArtEvent*)artEvent{
	PLSqliteDatabase *db = [database setup];
	
	//	int newsId = artEvent.news_id;
	int myId = artEvent._id;
	NSString* eventTitle = artEvent._title;
	NSString* thumb = artEvent._thumb;
	NSString* city = artEvent._city;
	int level = artEvent._level;
	NSString* startTime = artEvent._starttime;
	NSString* endTime = artEvent._endtime;
	NSString* address = artEvent._address;
	NSString* tel = artEvent._tel;
	NSString* googlePosi = artEvent._googleposi;
	NSString* content = artEvent._content;
	
	
	NSString* sql = [NSString stringWithFormat:@"update ArtEvent set title = '%@',thumb='%@',city='%@',level=%d,starttime='%@',endtime='%@',address='%@',tel='%@',googleposi='%@',content='%@' where id=%d",
					 eventTitle,
					 thumb,
					 city,
					 level,
					 startTime,
					 endTime,
					 address,
					 tel,
					 googlePosi,
					 content,
					 myId];
		
	BOOL re = [db executeUpdate:sql];
	return re;
}


+(int) updateWithNewsID:(int)newsId Title:(NSString *)title Thumb:(NSString *)thumb City:(NSString *)city Level:(int)level Starttime:(NSString *)start Endtime:(NSString *)end Address:(NSString *)add Tel:(NSString *)tel Googleposi:(NSString *)posi Content:(NSString *)content
{
	PLSqliteDatabase *db = [database setup];
	BOOL re = [db executeUpdate:@"update ArtEvent set title = ?,thumb=?,city=?,level=?,starttime=?,endtime=?,address=?,tel=?,googleposi=?,content=? where news_id=?",
			   title,
			   thumb,
			   city,
			   [NSNumber numberWithInt:level],
			   start,
			   end,
			   add,
			   tel,
			   posi,
			   content,
			   [NSNumber numberWithInt:newsId]];
	return re;
}

+(int) insertWithNewsID:(int)newsId Title:(NSString *)title Thumb:(NSString *)thumb City:(NSString *)city Level:(int)level Starttime:(NSString *)start Endtime:(NSString *)end Address:(NSString *)add Tel:(NSString *)tel Googleposi:(NSString *)posi Content:(NSString *)content
{
	PLSqliteDatabase *db = [database setup];
	BOOL re = 
	[db executeUpdate:@"insert into ArtEvent(news_id,title,thumb,city,level,starttime,endtime,address,tel,googleposi,content) values(?,?,?,?,?,?,?,?,?,?,?)",
			   [NSNumber numberWithInt:newsId],
			   title,
			   thumb,
			   city,
			   [NSNumber numberWithInt:level],
			   start,
			   end,
			   add,
			   tel,
			   posi,
			   content];
	return re;
}

+(int)delete:(int)newsid
{
	PLSqliteDatabase *db = [database setup];
	
	BOOL re = [db executeUpdate:@"delete from ArtEvent where news_id = ?",[NSNumber numberWithInt:newsid]];
	
	return re;
}



-(id) initWithID:(int)Id NewsID:(int)newsId Title:(NSString *)title Thumb:(NSString *)thumb City:(NSString *)city Level:(int)level Starttime:(NSString *)start Endtime:(NSString *)end Address:(NSString *)add Tel:(NSString *)tel Googleposi:(NSString *)posi Content:(NSString *)content
{
	if(self = [super init])
	{
		_id = Id;
		news_id = newsId;
		_title = [title retain];
		_thumb = [thumb retain];
		_city = [city retain];
		_level = level;
		_starttime = [start retain];
		_endtime = [end retain];
		_address = [add retain];
		_tel = [tel retain];
		_googleposi = [posi retain];
		_content = [content retain];
		
	}
	
	return self;
}



@end
